package umlms.server.service;

import umlms.server.conn.DefaultDispatcher;
import umlms.server.conn.IDispatcher;

public class UMLMSService implements RequestListener {

	public static final int TYPE_COMMAND = 0;

	public static final String COMMAND_STOP = "stop";
	public static final String COMMAND_GREET = "greet";
	public static final String COMMAND_HELP = "help";

	private Settings settings = new Settings();
	private IDispatcher dispatcher = new DefaultDispatcher();

	private boolean isRunning = false;

	private boolean isRunning() {
		return isRunning;
	}

	private void initialize(String[] args) {

		settings.initialize(args);
	}

	private void start() {
		isRunning = true;
	}

	public int getRequestType(String request) {
		return dispatcher.getRequestType(request);
	}

	public String dispatch(String request) {

		if (0 < dispatcher.getRequestType(request)) {
			return dispatcher.dispatch(request);
		}

		String requestLower = request.toLowerCase();
		if (requestLower.startsWith(COMMAND_HELP)) {

			StringBuilder helpBuilder = new StringBuilder();
			helpBuilder.append("Try UMLQL, UMLML, UMLDL or UMLCL"
					+ " ... or these commands:");
			helpBuilder.append("\n");
			helpBuilder.append("\"");
			helpBuilder.append(COMMAND_GREET);
			helpBuilder.append("\" -");
			helpBuilder.append(" for a warm welcome :-)");
			helpBuilder.append("\n");
			helpBuilder.append("\"");
			helpBuilder.append(COMMAND_STOP);
			helpBuilder.append("\" -");
			helpBuilder.append(" to stop the service :-(");
			helpBuilder.append("\n");
			helpBuilder.append("\"");
			helpBuilder.append(COMMAND_HELP);
			helpBuilder.append("\" -");
			helpBuilder.append(" to ... ummm ... show this message again.");
			helpBuilder.append("\n");

			String help = helpBuilder.toString();
			System.out.println(help);

			return help;
		} else if (requestLower.startsWith(COMMAND_STOP)) {
			System.out.println("Exiting");
			isRunning = false;
			System.out.println();
			System.out.println("stop;");
			return "terminated";
		} else if (requestLower.startsWith(COMMAND_GREET)) {
			System.out.println("GREETINGS!!!");
			return "greetings";
		} else {
			System.out.println("UNKNOWN: " + request);
			return "unknown";
		}
	}

	public static void main(String[] args) {

		UMLMSService service = new UMLMSService();
		UMLMSServer server = new UMLMSServer();

		try {

			service.initialize(args);
			service.start();

			server.initialize(service.getSettings());
			server.addRequestListener(service);
			Thread serverThread = new Thread(server);
			serverThread.start();

		} catch (RuntimeException exc) {
			System.out.println("Error: " + exc.getCause().getMessage());
			System.exit(1);
			return;
		}

		System.out.println("Service started on port: 9999");
		while (service.isRunning()) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}

		server.stop();
		System.exit(0);
	}

	private Settings getSettings() {
		return settings;
	}

	public String requestRecieved(String request) {
		return dispatch(request);
	}
}
